﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Net.Sockets;
using System.Net;
using System.Collections;
using System.Threading;
using System.Text.RegularExpressions;
using System.IO;
using ServerApp;

using PowerPoint = Microsoft.Office.Interop.PowerPoint;
using Core = Microsoft.Office.Core;
namespace ServerApp
{
   
    public class UDPServices
    {
        private PowerPoint.Application PPA;
        
        #region khoi tao 
        private string Data = "";
        private string folderPath;

        public string FolderPath
        {
            get { return folderPath; }
            set { folderPath = value; }
        }

        #endregion

        String testIP;
        public static Thread listenThread;

        public void server_Load()
        {
             listenThread = new Thread(tets);            
            
            listenThread.Start();
            listenThread.IsBackground = true;           
        }

        public String returnIP()
        {
            return testIP;
        }
        public string GetLocalIP()
        {
            string _IP = null;

            // Resolves a host name or IP address to an IPHostEntry instance.
            // IPHostEntry - Provides a container class for Internet host address information.
            System.Net.IPHostEntry _IPHostEntry = System.Net.Dns.GetHostEntry(System.Net.Dns.GetHostName());

            // IPAddress class contains the address of a computer on an IP network.
            foreach (System.Net.IPAddress _IPAddress in _IPHostEntry.AddressList)
            {
                // InterNetwork indicates that an IP version 4 address is expected
                // when a Socket connects to an endpoint
                if (_IPAddress.AddressFamily.ToString() == "InterNetwork")
                {
                    _IP = _IPAddress.ToString();
                }
            }
            return _IP;
        }
        private ControlPowerPoint control = new ControlPowerPoint();
        public string dsFilePowerPoint()
        {
            Form1 abc = new Form1();

            //string path = FolderPath;
            string path = @"D:\";
            DirectoryInfo dInfo = new DirectoryInfo(path);
            FileInfo[] files = dInfo.GetFiles();
            for (int i = 0; i < files.Length; i++)
            {
                string filename = files[i].Name;
                int length = filename.LastIndexOf(".");// lấy chiều dài của file
                // từ đầu đến dấu chấm
                String DuoiFile = filename.Substring(length + 1);
                if (DuoiFile.Equals("ppt") || DuoiFile.Equals("pptx"))
                {
                 //  string path_filename = path + filename;
                    Data += filename;
                    Data += ";";
                }
              //  MessageBox.Show(filename);


            }
            return Data;
        }
        public void tets()
        {
            string luutru = dsFilePowerPoint();
            MessageBox.Show(luutru);
            String filename = "";
            string[] a = null;
            int kq = 0;
            int recv;
            int port =8282;
            byte[] data = new byte[1400];
            string host = "";
            string IP = "";
            IPEndPoint ipendp = new IPEndPoint(IPAddress.Any, port);
            Socket newsock = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp);
            newsock.Bind(ipendp);
            EndPoint tmpRemote = (EndPoint)(ipendp);
            TCPservices tcpSv = new TCPservices();
            bool kt = false;
            host = GetLocalIP();
            while (!kt)
            {
                
                data = new byte[1400];
               recv = newsock.ReceiveFrom(data,ref tmpRemote);
               string test = Encoding.ASCII.GetString(data);
               testIP = test;
               a = Regex.Split(test, "\0");
               if (a[0] != "Open" && a[0] != "Next" && a[0] != "Previous" && a[0] != "Home" && a[0] != "End"
                   && a[0] != "SendFileName" && a[0] != "GoiDanhSach" && a[0] != "Disconect" && a[0] != "ShowFile")
               {
                   int length = a[0].LastIndexOf(".");// lấy chiều dài của file
                   // từ đầu đến dấu chấm
                   String DuoiFile = a[0].Substring(length + 1);
                   if (DuoiFile.Equals("ppt"))
                   {
                       filename = a[0];
                       
                   }
                   else
                   {
                       tcpSv.respones(a[0]);
                       IP = a[0];
                   }
                   
               }
               else
               {

                   string ThongTin = a[0];
                   if (ThongTin.Equals("Open"))
                   {
                       control.OpenPowerPoint(filename);
                   }
                   if (ThongTin.Equals("Next"))
                   {
                       control.Next();
                   }
                   if (ThongTin.Equals("Previous"))
                   {
                       control.Previous();
                   }
                   if (ThongTin.Equals("Home"))
                   {
                       control.Home();
                   }
                   if (ThongTin.Equals("End"))
                   {
                       control.End();
                   }
                   if (ThongTin.Equals("Disconect"))
                   {
                       control.Close();
                       kt = true;
                   }
                   if (ThongTin.Equals("SendFileName"))
                   {
                     
                       SendFileTCP send = new SendFileTCP();
                        filename = send.ReceiveName(host);
                      int ktt = send.ReceiveContentfileTCP(host,filename);
                       if (ktt == 1)
                       {
                           MessageBox.Show("Create file successful");
                       }
                       else
                       {
                           MessageBox.Show("Create file unsuccessful");
                       }
                   }
                   if (ThongTin.Equals("GoiDanhSach"))
                   {
                       tcpSv.respones(IP,luutru);
                   }

                   if (ThongTin.Equals("ShowFile"))
                   {
                         PPA = new PowerPoint.Application();
                        string listFile = "";
                        int t = PPA.Presentations.Count;

                        for (int i = 1; i <= t; i++)
                        {
                            listFile += PPA.Presentations[i].FullName + " - ";
                        }
                        string ten = PPA.Presentations[1].FullName;
                        filename = ten;
                   }
                
               }
              
              
            }
            newsock.Close();
            if (kt == true)
            {
                DialogResult dialogResult = MessageBox.Show("Bạn có muốn lưu file lại không ?", "Delete File Powerpoint", MessageBoxButtons.YesNo);
                if (dialogResult == DialogResult.Yes)
                {
                   
                }
                else if (dialogResult == DialogResult.No)
                {
                    string Path = "";
                    Path += @"D:\" + filename;
                    File.Delete(Path);
                }

            }
        }
        

        
    }
}
